<template>
  <div class="sys-vislog-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="state.queryParams" ref="queryForm" :inline="true">
        <el-form-item :label="$t('message.pageConfiguration.startTime')">
          <el-date-picker v-model="state.queryParams.startTime" type="datetime" :placeholder="$t('message.pageConfiguration.startTime')" value-format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts" />
        </el-form-item>
        <el-form-item :label="$t('message.pageConfiguration.endTime')" prop="code">
          <el-date-picker v-model="state.queryParams.endTime" type="datetime" :placeholder="$t('message.pageConfiguration.endTime')" value-format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts" />
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysVislog:page'"> {{$t('message.pageConfiguration.query')}} </el-button>
            <el-button icon="ele-Refresh" @click="resetQuery"> {{$t('message.pageConfiguration.reset')}} </el-button>
          </el-button-group>
        </el-form-item>
        <el-form-item>
          <el-button icon="ele-DeleteFilled" type="danger" @click="clearLog" v-auth="'sysVislog:clear'"> {{$t('message.pageConfiguration.clear')}} </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table :data="state.logData" style="width: 100%" v-loading="state.loading" border>
        <el-table-column type="index" :label="$t('message.pageConfiguration.number')" width="55" align="center" />
        <el-table-column prop="displayTitle" :label="$t('message.pageConfiguration.vName')" width="150" align="center" show-overflow-tooltip />
        <el-table-column prop="actionName" :label="$t('message.pageConfiguration.actionName')" width="150" header-align="center" show-overflow-tooltip />
        <el-table-column prop="account" :label="$t('message.pageConfiguration.accountNames')" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="realName" :label="$t('message.pageConfiguration.realName')" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="remoteIp" :label="$t('message.pageConfiguration.ip')" align="center" show-overflow-tooltip />
        <el-table-column prop="location" :label="$t('message.pageConfiguration.loginLocation')" align="center" show-overflow-tooltip />
        <el-table-column prop="browser" :label="$t('message.pageConfiguration.browser')" align="center" show-overflow-tooltip />
        <el-table-column prop="os" :label="$t('message.pageConfiguration.windows')" width="120" align="center" show-overflow-tooltip />
        <el-table-column prop="status" :label="$t('message.pageConfiguration.status')" width="70" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status === '200'">{{ $t('message.pageConfiguration.suc') }}</el-tag>
            <el-tag type="danger" v-else>{{ $t('message.pageConfiguration.fail') }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="elapsed" :label="$t('message.pageConfiguration.duration')" width="90" align="center" show-overflow-tooltip />
        <el-table-column prop="logDateTime" :label="$t('message.pageConfiguration.logTime')" width="160" align="center" fixed="right" show-overflow-tooltip />
      </el-table>
      <el-pagination v-model:currentPage="state.tableParams.page" v-model:page-size="state.tableParams.pageSize" :total="state.tableParams.total" :page-sizes="[10, 20, 50, 100]" small background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper" />
    </el-card>
  </div>
</template>

<script lang="ts" setup name="sysVisLog">
import { onMounted, reactive, ref, watch } from 'vue';
import { ElMessage } from 'element-plus';

import { getAPI } from '/@/utils/axios-utils';
import { SysLogVisApi } from '/@/api-services/api';
import { SysLogVis } from '/@/api-services/models';
import { useI18n } from 'vue-i18n';

const { t, locale } = useI18n();

const state = reactive({
	loading: false,
	queryParams: {
		startTime: undefined,
		endTime: undefined,
	},
	tableParams: {
		page: 1,
		pageSize: 20,
		total: 0 as any,
	},
	logData: [] as Array<SysLogVis>,
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	if (state.queryParams.startTime == null) state.queryParams.startTime = undefined;
	if (state.queryParams.endTime == null) state.queryParams.endTime = undefined;

	state.loading = true;
	let params = Object.assign(state.queryParams, state.tableParams);
	var res = await getAPI(SysLogVisApi).apiSysLogVisPagePost(params);
	state.logData = res.data.result?.items ?? [];
	state.tableParams.total = res.data.result?.total;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.startTime = undefined;
	state.queryParams.endTime = undefined;
	handleQuery();
};

// 清空日志
const clearLog = async () => {
	state.loading = true;
	await getAPI(SysLogVisApi).apiSysLogVisClearPost();
	state.loading = false;

	ElMessage.success(t('message.messages.clearSuccess'));
	handleQuery();
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	state.tableParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	state.tableParams.page = val;
	handleQuery();
};

const shortcuts = ref([
	{
		text: t('message.pageConfiguration.today'),
		value: new Date(),
	},
	{
		text: t('message.pageConfiguration.yesterday'),
		value: () => {
			const date = new Date();
			date.setTime(date.getTime() - 3600 * 1000 * 24);
			return date;
		},
	},
	{
		text: t('message.pageConfiguration.lastWeek'),
		value: () => {
			const date = new Date();
			date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
			return date;
		},
	},
]);
watch(locale, async () => {
	shortcuts.value = [
		{
			text: t('message.pageConfiguration.today'),
			value: new Date(),
		},
		{
			text: t('message.pageConfiguration.yesterday'),
			value: () => {
				const date = new Date();
				date.setTime(date.getTime() - 3600 * 1000 * 24);
				return date;
			},
		},
		{
			text: t('message.pageConfiguration.lastWeek'),
			value: () => {
				const date = new Date();
				date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
				return date;
			},
		},
	];
});
</script>
